package com.mdinfo.gw.device.stomp.controller;

import com.alibaba.fastjson.JSON;
import com.mdinfo.base.utils.TimeUtil;
import com.mdinfo.gw.device.stomp.bean.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

/**
 * Created by Administrator on 2017/12/19.
 */
@Controller
public class DemoController {

    private Logger logger = LoggerFactory.getLogger(DemoController.class);


    /**
     * 收到来自/app/send01 的消息
     * 并且发送消息到/topic/subscribe01
     */
    @MessageMapping("/send01")
    @SendTo("/topic/subscribe01")
    public ServerMessage sendDemo(ClientMessage clientMessage){
        logger.info("收到客户端的消息:"+clientMessage.getName());
        ServerMessage serverMessage = new ServerMessage();

        String msg = "你在<" + TimeUtil.formater(new Date())
                + ">发送的消息为:"
                + clientMessage.getName();

        serverMessage.setResponseMessage(msg);
        return serverMessage;
    }

    /**
     * 收到来自 /app/subscribe01 的主题订阅
     * @return
     */
    @SubscribeMapping("/subscribe01")
    public ServerMessage sub(){
        logger.info("有用户订阅了.");

        ServerMessage serverMessage = new ServerMessage();
        serverMessage.setResponseMessage("感谢你订阅了我..");
        return serverMessage;
    }

    /**
     * 收到来自 /topic/dept/{id}/alarm 的订阅消息
     * 并不能触发!!
     */
    @SubscribeMapping("/topic/dept/{id}/alarm")
    public ServerMessage subAlaram(@DestinationVariable int id){
        System.out.println(id);
        ServerMessage serverMessage = new ServerMessage();
        serverMessage.setResponseMessage("感谢你订阅了我..");
        return serverMessage;
    }



    @Resource
    private SimpMessagingTemplate template;


    /**
     * 接受http请求,并且发送stomp消息到指定的主题
     */
    @RequestMapping("/test/stomp/send")
    @ResponseBody
    public String sendMsg(){
        ServerMessage serverMessage = new ServerMessage();
        serverMessage.setResponseMessage("来自服务器的消息!");
        template.convertAndSend("/topic/send01",serverMessage);
        template.convertAndSend("/app/subscribe01",serverMessage);
        logger.info("服务器主动推送");
        return "ok";
    }

    /**
     * 接受http请求,并且发送stomp消息到指定的主题
     */
    @RequestMapping("/test/stomp/sendDept/{id}")
    @ResponseBody
    public String sendDept(@PathVariable Integer id){
        String msg = "来自服务器的消息,发送给"+id+"部门";
        ServerMessage serverMessage = new ServerMessage();
        serverMessage.setResponseMessage(msg);
        String dest = "/topic/dept/"+id+"/alarm";
        template.convertAndSend(dest,serverMessage);
        logger.info(msg);
        return "ok";
    }

    /**
     * 接受stomp消息
     */
    @MessageMapping("/dept/{deptId}/alarm/create")
    public void createAlarm(@DestinationVariable Integer deptId,
                                      AlarmClientMsg msg){

        Random random = new Random();
        int alarmId = random.nextInt(99) + 1;
        String content = "部门编号["+deptId+"],用户编号["+msg.getMemberId()+"]产生了一条报警";
        AlarmServerMsg serverMsg = new AlarmServerMsg();
        serverMsg.setAlarmId(alarmId);
        serverMsg.setContent(content);
        serverMsg.setCreateTime(TimeUtil.formater(new Date()));
        serverMsg.setDeptId(deptId);
        serverMsg.setMemberId(msg.getMemberId());
        serverMsg.setType(msg.getType());

        String dest = "/topic/dept/"+deptId+"/alarm";
//        template.convertAndSend(dest,serverMsg);
        AlarmMessage sendMsg = new AlarmMessage(deptId,
                msg.getMemberId(),
                msg.getType(),
                content,
                TimeUtil.formater(new Date()),
                alarmId);
        template.convertAndSend(dest,sendMsg);
    }

    /**
     * 接受处理报警
     */
    @MessageMapping("/dept/{deptId}/alarm/handle")
    public void handlerAlarm(@DestinationVariable Integer deptId,
                             AlarmClientMsg msg){

        System.out.println(deptId);
        System.out.println(JSON.toJSONString(msg));

        String content = "部门编号["+deptId+"],用户编号["+msg.getMemberId()
                +"]处理了编号["+msg.getAlarmId()+"]的报警";
        String dest = "/topic/dept/"+deptId+"/alarm";
        AlarmMessage sendMsg = new AlarmMessage(deptId,
                msg.getMemberId(),
                msg.getType(),
                content,
                TimeUtil.formater(new Date()),
                msg.getAlarmId());
        template.convertAndSend(dest,sendMsg);
    }
}
